﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace Rename.UC
{
    [ToolboxBitmap(typeof(TextBox))]
    public class UcTextBox : TextBox
    {
        private string _emptyTextTip;
        private Color _emptyTextTipColor = Color.DarkGray;
        private const int WM_PAINT = 0xF;
        public UcTextBox() : base()
        {
        }

        [DefaultValue("")]
        public string EmptyTextTip
        {
            get { return _emptyTextTip; }
            set
            {
                _emptyTextTip = value;
                base.Invalidate();
            }
        }

        [DefaultValue(typeof(Color), "DarkGray")]
        public Color EmptyTextTipColor
        {
            get { return _emptyTextTipColor; }
            set
            {
                _emptyTextTipColor = value;
                base.Invalidate();
            }
        }
        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //    using (GraphicsPath path = GetFigurePath(ClientRectangle, 10)) // 10是圆角的半径
        //    {
        //        this.Region = new Region(path);
        //    }
        //}
        //private GraphicsPath GetFigurePath(Rectangle rect, int radius)
        //{
        //    GraphicsPath path = new GraphicsPath();
        //    path.StartFigure();
        //    path.AddArc(rect.Left, rect.Top, radius, radius, 180, 90); // 左上角
        //    path.AddArc(rect.Right - radius, rect.Top, radius, radius, 270, 90); // 右上角
        //    path.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90); // 右下角
        //    path.AddArc(rect.Left, rect.Bottom - radius, radius, radius, 90, 90); // 左下角
        //    path.CloseFigure();
        //    return path;
        //}
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_PAINT)
            {
                WmPaint(ref m);
            }
        }
        private void WmPaint(ref Message m)
        {
            using (Graphics graphics = Graphics.FromHwnd(base.Handle))
            {
                if (Text.Length == 0
                    && !string.IsNullOrEmpty(_emptyTextTip)
                    && !Focused)
                {
                    TextFormatFlags format = TextFormatFlags.EndEllipsis | TextFormatFlags.Top;

                    if (RightToLeft == RightToLeft.Yes)
                    {
                        format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
                    }
                    Rectangle rect = base.ClientRectangle;
                    rect.X = 3;rect.Y = 3;
                    TextRenderer.DrawText(graphics, _emptyTextTip, Font, rect, _emptyTextTipColor, format);
                }
            }
        }
    }
}
